import { Credentials, SessionStore } from './types';
import SessionManager from './SessionManager';
import CookieSessionStore from './CookieSessionStore';
import MemorySessionStore from './MemorySessionStore';
import LocalStorageSessionStore, { localStorageSupported } from './LocalStorageSessionStore';
import * as API from './api';

let manager = new SessionManager();
function setStore(store: SessionStore): void {
    manager.setStore(store);
}

export function restoreSession(): Promise < void > {
    return manager.restoreSession();
}

export function importSession(): Promise < void > {
    return manager.refresh();
}

export function setCookieStore(sessionName: string): void {
    setStore(new CookieSessionStore(sessionName));
}

export function setLocalStorageStore(sessionName: string): void {
    localStorageSupported()
        ? setStore(new LocalStorageSessionStore(sessionName))
        : setStore(new MemorySessionStore());
}

export function session(): string | undefined {
    return manager.sessionToken();
}
export function endSesson(): void {
    return manager.endSession();
}
export function signup(credentials: Credentials): Promise < any > {
    return API
        .signup(credentials)
        .then(() => ({ response: 'success' }))
        .catch(error => ({ error }));
}

export function login(credentials: Credentials): Promise < any > {
    return API
        .login(credentials)
        .then((token) => {
            manager.update(token);
            return { response: token };
        })
        .catch(error => ({ error }));
}
export function loginSeccode(credentials: Credentials): Promise < any > {
    return API
        .loginSeccode(credentials)
        .then((token) => {
            manager.update(token);
            return { response: token };
        })
        .catch(error => ({ error }));
}
export function changePassword(args: {
oldPwd: string;
newPwd: string
}): Promise < any > {
    return API
        .changePassword(args)
        .then((token) => {
            manager.endSession();
            return { response: token };
        })
        .catch(error => ({ error }));
}

export function logout(): Promise < any > {
    return API
        .logout()
        .then(() => {
            manager.endSession();
            return { response: 'success' };
        })
        .catch(error => ({ error }));
}

export function resetPassword(credentials: Credentials): Promise < any > {
    return API
        .resetPassword(credentials)
        .then((token) => {
            manager.update(token);
            return { response: 'success' };
        })
        .catch(error => ({ error }));
}
export function userIdentify(args: any): Promise < any > {
    return API
        .identify(args)
        .then(() => ({ response: 'success' }))
        .catch(error => ({ error }));
}
// export remaining API methods unmodified
export { setHost, isAvailable, requestPasswordReset, seccode, getUser }
    from './api';
export { setUnauthorizedErrorHandler }
    from './verbs';